﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using Core;
using System.Threading.Tasks;
using System.Management;

namespace Service.Agent
{
    public partial class AgentService : ServiceBase
    {
        private static bool isCommandline = false;
        private Timer timer;

        public AgentService()
        {
            ServiceName = "SqlMonitoring.Agent";
        }

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                foreach (string option in args)
                {
                    switch (option.ToLower(CultureInfo.InvariantCulture))
                    {
                        case "d":
                        case "-d":
                        case "debug":
                        case "-debug":
                            isCommandline = true;
                            break;
                    }
                }
            }

            if (isCommandline)
            {
                Logging.LogInformation("Starting from Commandline");
                AgentService agentService = new AgentService();
                agentService.StartService();
                Thread.Sleep(Timeout.Infinite);
            }
            else
            {
                ServiceBase.Run(new AgentService());
            }
        }

        protected override void OnStart(string[] args)
        {
            MainService main = new MainService();
            main.IsCommandLine = isCommandline;
            main.Init();
            TimerCallback timerDelegate =
                new TimerCallback(main.Checks);

            timer = new System.Threading.Timer(timerDelegate, null, 1000, 60000);

            // This will watch the event log and get new events. These will be matched against a list of ID's to watch
            // Another possibility is to do a WMI query during each loop getting the data...
            // Still food for thought
            //ManagementEventWatcher mew = new ManagementEventWatcher(
            //    "SELECT * FROM __instanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' AND TargetInstance.LogFile='Application'");
            //mew.EventArrived += new EventArrivedEventHandler(mew_EventArrived);
            //mew.Start();
        }

        void mew_EventArrived(object sender, EventArrivedEventArgs e)
        {
            //Logging.LogInformation("*!@* Event arrived");
            //ManagementBaseObject ev = e.NewEvent;
            //Logging.LogInformation( ((ManagementBaseObject)e.NewEvent["TargetInstance"])["Message"].ToString());
        }

        protected override void OnStop()
        {

        }

        public void StartService()
        {
            OnStart(new string[] { });
        }

        public void StopService()
        {
            OnStop();
        }
    }
}
